package com.honglang.module.album.action;

import java.util.ArrayList;
import java.util.List;

import com.honglang.common.tools.Constants;
import com.honglang.module.album.entity.Album;
import com.honglang.module.album.entity.AlbumPhoto;
import com.honglang.module.album.service.AlbumPhotoService;
import com.honglang.module.album.service.AlbumService;
import com.honglang.module.album.service.PhotoService;
import com.honglang.module.user.entity.User;
import com.honglang.struts2.action.BaseAction;
import com.opensymphony.xwork2.Action;

public class AlbumManagerAction extends BaseAction {
	private AlbumService albumService;
	private AlbumPhotoService albumPhotoService;
	private PhotoService photoService;
	private List<String> photoId;
	private List<String> desc;
	private List<String> albumPhotoId;

	public String preCreateAlbum() {
		String pageNoStr = this.getRequest().getParameter("pageNo");
		int pageNo = Constants.DEFAULT_PAGE_NO;
		if (null != pageNoStr) {
			pageNo = Integer.valueOf(this.getRequest().getParameter("pageNo"));
		}
		this.getRequest().setAttribute("pager",
				this.photoService.getPager(Constants.PHOTO_PAGE_SIZE, pageNo));
		return Action.SUCCESS;
	}

	/**
	 * get one pager of photo
	 * 
	 * @return
	 */
	public String ajaxGalleria() {
		String pageNoStr = this.getRequest().getParameter("pageNo");
		int pageNo = Constants.DEFAULT_PAGE_NO;
		if (null != pageNoStr) {
			pageNo = Integer.valueOf(this.getRequest().getParameter("pageNo"));
		}
		this.getRequest().setAttribute("pager",
				this.photoService.getPager(Constants.PHOTO_PAGE_SIZE, pageNo));

		return Action.SUCCESS;
	}

	/**
	 * create Album
	 * 
	 * @return
	 */
	public String createAlbum() {

		String albumTitle = this.getRequest().getParameter("albumTitle");
		Album album = new Album();
		album.setAuthor((User) this.getSession().getAttribute(
				Constants.USER_KEY));
		album.setTitle(albumTitle);
		album = this.getAlbumService().save(album);
		if (photoId != null) {
			List<AlbumPhoto> albumPhotoList = new ArrayList<AlbumPhoto>();
			for (int i = 0; i < photoId.size(); i++) {
				AlbumPhoto albumPhoto = new AlbumPhoto();
				albumPhoto.setAlbum(album);
				albumPhoto.setDescription(desc.get(i));
				albumPhoto.setPhoto(this.getPhotoService().getPhoto(
						Integer.valueOf(photoId.get(i))));
				albumPhotoList.add(albumPhoto);

			}
			this.getAlbumPhotoService().save(albumPhotoList);
			album.setPhotoNumber(albumPhotoList.size());
			this.getAlbumService().save(album);
		}
		return Action.SUCCESS;
	}

	/**
	 * prepare manager album
	 * 
	 * @return
	 */
	public String preAlbumManager() {
		String pageNoStr = this.getRequest().getParameter("pageNo");
		int pageNo = (null != pageNoStr) ? Integer.valueOf(this.getRequest()
				.getParameter("pageNo")) : Constants.DEFAULT_PAGE_NO;
		this.getRequest().setAttribute("pager",
				this.albumService.getPager(Constants.PHOTO_PAGE_SIZE, pageNo));
		return Action.SUCCESS;
	}

	/**
	 * delte album
	 * 
	 * @return
	 */
	public String deleteAlbum() {
		System.out.println("00000000000000000000");
		String albumIds = this.getRequest().getParameter("aid");
		if (null != albumIds && !albumIds.isEmpty()) {
			if (albumIds.split(Constants.Album_Split).length == 1) {
				this.getAlbumService().deleteAlbum(Integer.valueOf(albumIds));
			} else {
				this.getAlbumService().batchDeleteAlbum(albumIds);
			}
		}
		return Action.NONE;
	}

	/**
	 * prepare edit album
	 * 
	 * @return
	 */
	public String preEditAlbum() {
		String aid = this.getRequest().getParameter("aid");

		List<AlbumPhoto> albumPhotoList = this.getAlbumPhotoService()
				.getAlbumPhotoByAlbumId(Integer.valueOf(aid));

		this.getRequest().setAttribute("albumPhotoList", albumPhotoList);
		this.getRequest().setAttribute("album",
				this.getAlbumService().getById(Integer.valueOf(aid)));

		return Action.SUCCESS;
	}

	/**
	 * edit album
	 * 
	 * @return
	 */
	public String editAlbum() {
		String albumTitle = this.getRequest().getParameter("albumTitle");
		String albumId = this.getRequest().getParameter("albumId");
		Album album = this.getAlbumService().getById(Integer.valueOf(albumId));
		album.setTitle(albumTitle);
		album = this.getAlbumService().save(album);
		if (albumPhotoId != null) {
			List<AlbumPhoto> albumPhotoList = new ArrayList<AlbumPhoto>();
			for (int i = 0; i < albumPhotoId.size(); i++) {
				AlbumPhoto albumPhoto = this.getAlbumPhotoService().getById(
						Integer.valueOf(albumPhotoId.get(i)));
				albumPhoto.setDescription(desc.get(i));
				albumPhotoList.add(albumPhoto);

			}
			this.getAlbumPhotoService().save(albumPhotoList);
		}
		return Action.SUCCESS;
	}

	/**
	 * remove photo from album
	 * 
	 * @return
	 */
	public String removePhotoFromAlbum() {
		String albumPhotoId = this.getRequest().getParameter("apId");
		AlbumPhoto albumPhoto = this.getAlbumPhotoService().getById(
				Integer.valueOf(albumPhotoId));
		Album album = this.getAlbumService().getById(
				albumPhoto.getAlbum().getId());
		this.getAlbumPhotoService().delete(albumPhoto);
		album.setPhotoNumber(album.getPhotoNumber() - 1);
		this.getAlbumService().save(album);
		return Action.NONE;
	}

	public PhotoService getPhotoService() {
		return photoService;
	}

	public void setPhotoService(PhotoService photoService) {
		this.photoService = photoService;
	}

	public AlbumService getAlbumService() {
		return albumService;
	}

	public void setAlbumService(AlbumService albumService) {
		this.albumService = albumService;
	}

	public AlbumPhotoService getAlbumPhotoService() {
		return albumPhotoService;
	}

	public void setAlbumPhotoService(AlbumPhotoService albumPhotoService) {
		this.albumPhotoService = albumPhotoService;
	}

	public List<String> getPhotoId() {
		return photoId;
	}

	public void setPhotoId(List<String> photoId) {
		this.photoId = photoId;
	}

	public List<String> getDesc() {
		return desc;
	}

	public void setDesc(List<String> desc) {
		this.desc = desc;
	}

	public List<String> getAlbumPhotoId() {
		return albumPhotoId;
	}

	public void setAlbumPhotoId(List<String> albumPhotoId) {
		this.albumPhotoId = albumPhotoId;
	}

}
